package com.cy.pj.module.controller.web;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

//@ResponseBody
//@ControllerAdvice //此注解描述的类为spring web模块中的全局异常处理的类
@RestControllerAdvice
//@ResponseBody+@ControllerAdvice=@RestControllerAdvice
//@ResponseBody+@Controller=@RestController
public class GlobalExceptionHandler {


    @ExceptionHandler(ArithmeticException.class)//描述方法为异常处理方法
    @ResponseBody
    public String doHandleArithmeticException(/*Arithmetic*/Exception e){//算数异常 参数可以放父类异常
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler ");
        return "计算过程中出现了异常,异常信息为"+e.getMessage();
    }

    @ExceptionHandler(RuntimeException.class)//描述方法为异常处理方法
    @ResponseBody
    public String doHandleArithmeticException(RuntimeException e){//算数异常 参数可以放父类异常
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler ");
        return "运行过程中出现了异常,异常信息为"+e.getMessage();
    }

    @ExceptionHandler(Throwable.class)//总异常
    @ResponseBody
    public String doHandleArithmeticException(Throwable e){//算数异常 参数可以放父类异常
        e.printStackTrace();
        System.out.println("GlobalExceptionHandler ");
        //发邮件,发短信,服务端后台报警(播放一段音乐)
        return "系统维护中";
    }

}
